<!--微信账号-->
<template>
  <div class="app-container">
    <div class="search">
      <el-input
        class="search-input"
        v-model="queryParams.key"
        placeholder="请输入微信昵称"
        @change="getWeChatList"
      ></el-input>
      <el-button type="primary" icon="el-icon-plus" @click="goClick(0)"
        >新增微信账号</el-button
      >
    </div>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column
        label="微信Id"
        align="center"
        prop="wxId"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="微信昵称"
        align="center"
        prop="wxName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="推荐人Id"
        align="center"
        prop="refereeWxId"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="保险人Id"
        align="center"
        prop="insurerWxId"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="goClick(1, scope.row)"
            >修改</el-button>

          <el-button
            size="mini"
            type="text"
            @click="goWxInfo(scope.row)"
            >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getWeChatList"
    />
    <el-dialog :title="title" :visible.sync="dialogVisible" width="500px">
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-form-item label="微信Id" prop="wxId">
          <el-input v-model="form.wxId" clearable></el-input>
        </el-form-item>
        <el-form-item label="微信昵称" prop="wxName">
          <el-input v-model="form.wxName" clearable></el-input>
        </el-form-item>
        <el-form-item label="关联推荐人">
          <el-select
            style="width: 100%"
            v-model="form.refereeWxId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="selectloading"
            clearable
          >
            <el-option
              v-for="item in options"
              :key="item.wxId"
              :label="item.wxId + ' ' + item.wxName"
              :value="item.wxId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联保险人">
          <el-select
            style="width: 100%"
            v-model="form.insurerWxId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="selectloading"
            clearable
          >
            <el-option
              v-for="item in options"
              :key="item.wxId"
              :label="item.wxId + ' ' + item.wxName"
              :value="item.wxId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="5"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getWeChatList,
  goAddWeChat,
  goUpdateWeChat,
  goDeleteWeChat,
  goSearchWeChat,
} from "@/api/account/wx.js";
export default {
  data() {
    return {
      queryParams: {
        key: "",
        pageNum: 1,
        pageSize: 10,
      },
      loading: false,
      tableData: [],
      total: 0,
      title: "",
      dialogVisible: false,
      form: {
        wxId: "",
        wxName: "",
        refereeWxId: "",
        insurerWxId: "",
        remark: "",
      },
      rules: {
        wxId: [{ required: true, message: "请输入微信Id", trigger: "blur" }],
        wxName: [
          { required: true, message: "请输入微信昵称", trigger: "blur" },
        ],
      },
      selectloading: false,
      options: [],
    };
  },
  mounted() {
    this.getWeChatList();
  },
  methods: {
    // 查询微信账号列表
    getWeChatList() {
      this.loading = true;
      getWeChatList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 操作
    goClick(type, row) {
      switch (type) {
        case 0:
          this.reset();
          this.title = "新增微信账号";
          this.dialogVisible = true;
          break;
        case 1:
          this.form = { ...row };
          this.title = "编辑微信账号";
          this.dialogVisible = true;
          break;
        case 2:
          this.form = { ...row };
          this.$confirm("确定要删除微信Id为" + row.wxId + "？", "提示", {
            type: "warning",
          }).then(() => {
            this.goDeleteWeChat();
          });
          break;
      }
    },
    // 保存
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title == "新增微信账号") {
            this.goAddWeChat();
          } else {
            this.goUpdateWeChat();
          }
        } else {
          return false;
        }
      });
    },
    // 新增微信账号
    goAddWeChat() {
      goAddWeChat(this.form).then((res) => {
        this.$message.success("操作成功");
        this.dialogVisible = false;
        this.getWeChatList();
      });
    },
    // 编辑微信账号
    goUpdateWeChat() {
      goUpdateWeChat(this.form).then((res) => {
        this.$message.success("操作成功");
        this.dialogVisible = false;
        this.getWeChatList();
      });
    },
    // 删除微信账号
    goDeleteWeChat() {
      goDeleteWeChat(this.form).then((res) => {
        this.$message.success("操作成功");
        this.getWeChatList();
      });
    },
    // 重置表单
    reset() {
      this.form = {
        wxId: "",
        wxName: "",
        refereeWxId: "",
        insurerWxId: "",
        remark: "",
      };
      this.resetForm("form");
    },
    // 跳转微信详情
    goWxInfo(row) {
      this.$router.push({
        path: "/player/wxDetail",
        query: { id: row.id },
      });
    },
    // 模糊查询保险人/推荐人
    remoteMethod(query) {
      if (query !== "") {
        this.selectloading = true;
        goSearchWeChat({ key: query }).then((res) => {
          this.options = res.rows;
          this.selectloading = false;
        });
      } else {
        this.options = [];
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.search {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  .search-input {
    width: 240px;
  }
}
</style>
